/**
 * BeadSet.java
 * Handles graphics assets for beads.
 * 
 * Copyright (c) 2012 The Javanauts
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package edu.utsa.cs.cs4953_2012.abacus;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Loads and organizes the myriad graphics assets of bead states.
 * 
 */
public class BeadSet {
	/**
	 * Set of images for the red ones.
	 */
	private BufferedImage[] ones_1;
	/**
	 * Set of images for the yellow ones.
	 */
	private BufferedImage[] ones_2;
	/**
	 * Set of images for the purple ones.
	 */
	private BufferedImage[] ones_3;

	/**
	 * Set of images for the red fives.
	 */
	private BufferedImage[] fives_1;
	/**
	 * Set of images for the yellow fives.
	 */
	private BufferedImage[] fives_2;
	/**
	 * Set of images for the purple fives.
	 */
	private BufferedImage[] fives_3;

	/**
	 * Constructor
	 * 
	 * @param dir
	 *            The directory under images/beads where the assets lie.
	 */
	public BeadSet(String dir) {
		ones_1 = new BufferedImage[5];
		fives_1 = new BufferedImage[4];
		ones_2 = new BufferedImage[5];
		fives_2 = new BufferedImage[4];
		ones_3 = new BufferedImage[5];
		fives_3 = new BufferedImage[4];

		// load the images for the ones' blocks.
		try {
			for (int i = 0; i < ones_1.length; i++)
				ones_1[i] = ImageIO.read(new File("images/beads/" + dir
						+ "/1blockCase" + i + "_0.png"));
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			ones_1 = null;
		}
		try {
			for (int i = 0; i < ones_2.length; i++)
				ones_2[i] = ImageIO.read(new File("images/beads/" + dir
						+ "/1blockCase" + i + "_1.png"));
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			ones_2 = null;
		}
		try {
			for (int i = 0; i < ones_3.length; i++)
				ones_3[i] = ImageIO.read(new File("images/beads/" + dir
						+ "/1blockCase" + i + "_2.png"));
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			ones_3 = null;
		}

		// load the images for the fives' blocks.
		try {
			for (int i = 0; i < fives_1.length; i++)
				fives_1[i] = ImageIO.read(new File("images/beads/" + dir
						+ "/5blockCase" + i + "_0.png"));
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			fives_1 = null;
		}
		try {
			for (int i = 0; i < fives_2.length; i++)
				fives_2[i] = ImageIO.read(new File("images/beads/" + dir
						+ "/5blockCase" + i + "_1.png"));
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			fives_2 = null;
		}
		try {
			for (int i = 0; i < fives_3.length; i++)
				fives_3[i] = ImageIO.read(new File("images/beads/" + dir
						+ "/5blockCase" + i + "_2.png"));
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			fives_3 = null;
		}
	}

	/**
	 * Gets the appropriate set of images for the fives' block of a particular
	 * row.
	 * 
	 * @param i
	 *            The row in question.
	 * @return The images the fives' block will use.
	 */
	public BufferedImage[] getFivesImagesForRow(int i) {
		if (i < 0 || i >= 13)
			throw new IllegalArgumentException("Only rows 0-12 are valid.");
		switch (i) {
		case 0:
		case 1:
		case 2:
		case 6:
		case 7:
		case 8:
			return fives_1;
		case 3:
		case 4:
		case 5:
		case 9:
		case 10:
		case 11:
			return fives_2;
		default:
			return fives_3;
		}
	}

	/**
	 * Gets the appropriate set of images for the ones' block of a particular
	 * row.
	 * 
	 * @param i
	 *            The row in question.
	 * @return The images the ones' block will use.
	 */
	public BufferedImage[] getOnesImagesForRow(int i) {
		if (i < 0 || i >= 13)
			throw new IllegalArgumentException("Only rows 0-12 are valid.");
		switch (i) {
		case 0:
		case 1:
		case 2:
		case 6:
		case 7:
		case 8:
			return ones_1;
		case 3:
		case 4:
		case 5:
		case 9:
		case 10:
		case 11:
			return ones_2;
		default:
			return ones_3;
		}
	}
}
